<template>
	<om-formsub-outer :index="index" :checkedit="checkedit">
		<om-autoform ref="autoform" :edit="edit" :checkedit="checkedit" :initConfigData="autoformdata" :initValue="initValue"></om-autoform>
	</om-formsub-outer>
</template>

<script>
	import {
		buildSubFormData,
		subOnLoad,
		changeEditor,
		configSelectData,
		hasValue
	} from '../../../static/js/formUtil.js';
	import {
		ignoreHtml,
		errorModal,
		minusF,
		getDateStr
	} from '@/static/js/common.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import moment from"@/static/js/moment.js";
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildSubFormData({
				zposId: "",
				busCode: '',
				exists: [],
				reasonCache: "hr-u-supply-titleInfo-reason-cache",
				posCacheKey:"hr-u-supply-titleInfo-pos-cache",
				dayNum:0,
				formCode: ""
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId'])
		},
		methods: {
			getParentUserNameByPosId(id){
				let _that = this
				_that.om.request(
					"tpeopleAddApply/getDataAfterPos",
					{
						posId: id
					}
				).then(res=>{
					if (res.succeed) {
					    var value = res.value;
					    if (value) {
							_that.$refs.autoform.formdata.parentUserId = value.parentUserId
							_that.$refs.autoform.formdata.parentUserName = value.parentUserName
							_that.$refs.autoform.formdata.existNum = value.existNum
							_that.$refs.autoform.clear("titleInfo","hopeArriveDate")
							_that.$refs.autoform.clear("titleInfo","finalArriveDate")
					    }
					} else {
						_that.om.toast(res.msg)
					}
				})
			},
			onSelectHopeArriveDate(time){
				let _that = this
				if(_that.$refs.autoform.formdata.finalArriveDate){
					let isBefore = moment(_that.$refs.autoform.formdata.finalArriveDate).isBefore(time)
					let finalArriveTitle = _that.$refs.autoform.getEditorTitle("titleInfo","finalArriveDate")
					let hopeArriveTitle = _that.$refs.autoform.getEditorTitle("titleInfo","hopeArriveDate")
					if(isBefore){
						errorModal(_that,"提示信息",hopeArriveTitle+"必须小于"+finalArriveTitle,()=>{
							_that.$refs.autoform.clear("titleInfo","hopeArriveDate")
						})
					}
				}
				for (var i = 0; i < _that.datas.length; i++) {
				    if (_that.index === i) {
				        continue;
				    }
				    var rowData = _that.datas[i];
				    if (!rowData.posId) {
				        continue;
				    }
				    if (rowData.posId == _that.$refs.autoform.formdata.posId && (rowData.hopeArriveDate == time || rowData.finalArriveDate == time)) {
				        errorModal(_that,"提示信息","同一个岗位不能选择相同到岗时间",()=>{
				        	_that.$refs.autoform.clear("titleInfo","hopeArriveDate")
				        })
				        return true;
				    }
				}
				_that.om.request('tpeopleAddApply/verifyPosArriveDate', {
					formCode: _that.formCode,
					posId: _that.$refs.autoform.formdata.posId,
					date: time
				}, 'post', true).then(data => {
					if (data.value.succeed) {
						errorModal(_that,"提示信息",data.value.msg,()=>{
							_that.$refs.autoform.clear("titleInfo","hopeArriveDate")
						})
					}else{
						if(_that.dayNum){
							var now = new Date();
							var date = new Date(time);
							var limitDate = new Date(now.getFullYear(), now.getMonth(), now.getDate() + _that.dayNum);
							if (date.getTime() < limitDate.getTime()) {
							    var msg = "管控参数配置中做了约束，“期望到岗日期”只能选择当前日期 " + _that.dayNum + " 天之后的日期，请重新选择！";
							    errorModal(_that,"提示信息",msg,()=>{
							    	_that.$refs.autoform.clear("titleInfo","hopeArriveDate")
							    })
							}
						}
					}
				});
			},
			onSelectFinalArrive(time){
				let _that = this
				if(_that.$refs.autoform.formdata.hopeArriveDate){
					let isBefore = moment(time).isBefore(_that.$refs.autoform.formdata.hopeArriveDate)
					let finalArriveTitle = _that.$refs.autoform.getEditorTitle("titleInfo","finalArriveDate")
					let hopeArriveTitle = _that.$refs.autoform.getEditorTitle("titleInfo","hopeArriveDate")
					if(isBefore){
						errorModal(_that,"提示信息",finalArriveTitle+"必须大于"+hopeArriveTitle,()=>{
							_that.$refs.autoform.clear("titleInfo","finalArriveDate")
						})
					}
				}
				for (var i = 0; i < _that.datas.length; i++) {
				    if (_that.index === i) {
				        continue;
				    }
				    var rowData = _that.datas[i];
				    if (!rowData.posId) {
				        continue;
				    }
				    if (rowData.posId == _that.$refs.autoform.formdata.posId && (rowData.hopeArriveDate == time || rowData.finalArriveDate == time)) {
				        errorModal(_that,"提示信息","同一个岗位不能选择相同到岗时间",()=>{
				        	_that.$refs.autoform.clear("titleInfo","finalArriveDate")
				        })
				        return true;
				    }
				}
				_that.om.request('tpeopleAddApply/verifyPosArriveDate', {
					formCode: _that.formCode,
					posId: _that.$refs.autoform.formdata.posId,
					date: time
				}, 'post', true).then(data => {
					if (data.value.succeed) {
						errorModal(_that,"提示信息",data.value.msg,()=>{
							_that.$refs.autoform.clear("titleInfo","finalArriveDate")
						})
					}
				});
			},
			getIntByCode(){
				let _that = this
				_that.om.request(
					"dataDict/getIntByCode",
					{
						code:"DD014008001"
					}
				).then(res=>{
					_that.dayNum = res.value
				})
			},
			onSelectStartDate(item){
				let _that = this
					let day = ""
					let day1 = moment()
					let day2 = moment(item)
					day = day2.diff(day1, 'day')
					if(Number(_that.dayNum) === 0){
						if((Number(day)) < Number(_that.dayNum)){
							errorModal(_that,"提示信息","管控参数约束需要选择当前日期"+_that.dayNum+"天之后的日期，请重新选择！",function(){
							_that.$refs.autoform.clear("titleInfo","hopeArriveDate")
						})
					}	
				}else{
					if((Number(day)+1) < Number(_that.dayNum)){
							errorModal(_that,"提示信息","管控参数约束需要选择当前日期"+_that.dayNum+"天之后的日期，请重新选择！",function(){
							_that.$refs.autoform.clear("titleInfo","hopeArriveDate")
						})
					}	
				}
				if(_that.$refs.autoform.formdata.finalArriveDate){
					_that.$refs.autoform.clear("titleInfo","finalArriveDate")
				}
			},
			// verifyReason(code,dicname) {
			// 	let _that = this
			// 	_that.om.request('hrPosUtil/verifyReasonCodeByH5', {
			// 		zposId: _that.zposId,
			// 		reasonCode: code,
			// 		busCode: _that.busCode
			// 	}, 'post').then(res => {
			// 		if (res.succeed && res.value.field) {
			// 			errorModal(_that,"提示信息","当选择"+dicname+"时，会约束选择的岗位。",()=>{
			// 				_that.$refs.autoform.changeEditor("titleInfo","posId","data",configSelectData(res.value.attributes.pos,"id","posname"))
							
			// 			})
			// 		}
			// 	})
			// },
			// clearReason() {
			// 	this.$refs.autoform.clear("titleInfo", "reasonCode")
			// 	this.clearPos()
			// },
			onSelectPos(item) {
				this.$refs.autoform.formdata.posId = item.posid
				this.$refs.autoform.formdata.posName = item.posname
				this.$refs.autoform.formdata.instShort = item.instname
				this.$refs.autoform.formdata.posGrade = item.careercat
				this.$refs.autoform.formdata.salaryWay = item.saraycat
			},
			clearPos() {
				this.$refs.autoform.clearGroup("titleInfo")
			},
			initLoad(edit) {
				let _that = this
				let ajaxArr = [
					_that.om.request(
						"dictZero/getListByCode",
						{
							code:'23868'
						}
					),
					_that.om.request(
						"dictZero/getListByCode",
						{
							code:'23591'
						}
					),
					_that.om.request(
						"dictZero/getListByCode",
						{
							code:'37861'
						}
					),
					_that.om.request(
						"tsysdict/getCombobox",
						{
							code:'38941'
						}
					),
					_that.om.request(
						"tsysdict/getCombobox",
						{
							code:'38945'
						}
					),
					_that.om.request(
						"dictZero/getListByCode",
						{
							code:'38400'
						}
					),
					_that.om.request(
						"tsysdict/getCombobox",
						{
							code:'38950'
						}
					),
				]
				if(edit ==="true"){
					// ajaxArr.push(
					// _that.om.request(
					// 		'hrPosUtil/verifyReasonCodeByH5',
					// 		{
					// 			zposId: _that.zposId,
					// 			reasonCode: _that.formdata.reasonCode,
					// 			busCode: _that.busCode
					// 		},
					// 		"post"
					// ))
				}
				Promise.all(ajaxArr).then(res => {
					// _that.$refs.autoform.changeEditor("titleInfo", "eduCode", "data", configSelectData(res[0], "code", "dicname"))
					// _that.$refs.autoform.initSelectIndex("titleInfo", "eduCode")
					// _that.$refs.autoform.changeEditor("titleInfo", "sexCode", "data", configSelectData(res[1], "code", "dicname"))
					// _that.$refs.autoform.initSelectIndex("titleInfo", "sexCode")
					_that.$refs.autoform.changeEditor("titleInfo", "budgetTypeId", "data", configSelectData(res[2], "code", "dicname"))
					_that.$refs.autoform.initSelectIndex("titleInfo", "budgetTypeId")
					_that.$refs.autoform.changeEditor("titleInfo", "applyTypeId", "data", configSelectData(res[3], "code", "dicname"))
					_that.$refs.autoform.initSelectIndex("titleInfo", "applyTypeId")
					_that.$refs.autoform.changeEditor("titleInfo", "addSourceId", "data", configSelectData(res[4], "code", "dicname"))
					_that.$refs.autoform.initSelectIndex("titleInfo", "addSourceId")
					_that.$refs.autoform.changeEditor("titleInfo", "technicalTestId", "data", configSelectData(res[5], "code", "dicname"))
					_that.$refs.autoform.initSelectIndex("titleInfo", "technicalTestId")
					_that.$refs.autoform.changeEditor("titleInfo", "urgencyId", "data", configSelectData(res[6], "code", "dicname"))
					_that.$refs.autoform.initSelectIndex("titleInfo", "urgencyId")
				})
			}
		},
		onLoad(e) {
			let _that = this
			this.zposId = e.zposId
			this.busCode = e.busCode
			this.formCode = uni.getStorageSync('formCodeNum')
			subOnLoad(_that, e, function() {
				if (e.configDataCacheKey) {
					_that.exists = _that.datas.filter((e, i) => {
						return (i != _that.index)
					}).map(e => e.interviewElementId).join(",")
					uni.getStorage({
						key: e.configDataCacheKey,
						success: function(res) {
							//if(res.data && res.data instanceof Array){
							if (res.data && Object.prototype.toString.call(res.data) === '[object Array]') {
								_that.autoformdata = res.data
								Object.assign(_that.initValue, _that.formdata)
								_that.autoformdata[0].order = _that.order
								let d = new Date()
								changeEditor("titleInfo","hopeArriveDate","min",getDateStr(d),_that.autoformdata)
								changeEditor("titleInfo","finalArriveDate","startField","hopeArriveDate",_that.autoformdata)
								// changeEditor("titleInfo", "reasonCode", {
								// 	link: '../../public/pageSearch/pageSearch',
								// 	"linkParam": {
								// 		other: JSON.stringify({}),
								// 		namefield: 'dicname',
								// 		initload: true,
								// 		cachename: _that.reasonCache,
								// 		url: 'tpeopleAddApply/getNewReasonCodeList'
								// 	}
								// }, null, _that.autoformdata)
								changeEditor("titleInfo", "posId", {
									link: '../../public/pageSearch/pageSearch',
									"linkParam": {
										other:JSON.stringify({
											userId: _that.userId,
											businessFormCode: 'HRS0801'
										}),
										namefield: 'posName',
										notefield:'instName',
										searchfield:'q',
										initload: true,
										cachename: _that.posCacheKey,
										url: 'tposition/getPostPagerByUser'
									}
								}, null, _that.autoformdata)
								_that.getIntByCode()
								_that.initLoad(e.edit)	
							}
						}
					});
				}
			})
		},
		onShow() {
			let _that = this
			if (_that.$refs.autoform) {
				// uni.getStorage({
				// 	key: _that.reasonCache,
				// 	success: (res) => {
				// 		if (res.data) {
				// 			let d = res.data
				// 			let formdata = _that.$refs.autoform.formdata
				// 			formdata.reasonCode = d.code
				// 			formdata.reasonName = d.dicname
				// 			// _that.verifyReason(d.code,d.dicname)
				// 			uni.removeStorage({
				// 				key: _that.reasonCache
				// 			})
				// 		}
				// 	}
				// })
				uni.getStorage({
					key: _that.posCacheKey,
					success: (res) => {
						if (res.data) {
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.posId = d.id
							formdata.posName = d.posName
							formdata.salaryWay = d.salarycatName
							formdata.instShort = d.instShortName
							formdata.posGrade = d.careercatName
							formdata.planNum = d.plansum
							_that.getParentUserNameByPosId(d.id)
							uni.removeStorage({
								key: _that.posCacheKey
							})
						}
					}
				})
			}
		}
	}
</script>

<style>

</style>
